<template>
	<view class="wrap">
		<view class="main">
			<view class="user">
				<view class="user-avatar">
					<image :src="gShopInfo.logoImg"></image>
				</view>
				<view class="user-R">
					<view class="username fw">{{gShopInfo.name}}</view>
					<view class="status" v-if="!isVipFlag">未开通</view>
					<view class="status" v-else>{{gUserInfo.levelName}}</view>
				</view>
			</view>
			<view class="plan">
				<view class="plan-header">选择续费套餐</view>
				<view class="plan-main">
					<view class="plan-item" :class="{active: count==1}" @click="count=1">
						<view>1个月</view>
						<view>
							<text>￥</text>
							<text>35</text>
						</view>
						<view>￥38</view>
					</view>
					<view class="plan-item" :class="{active: count==6}" @click="count=6">
						<view>6个月</view>
						<view>
							<text>￥</text>
							<text>198</text>
						</view>
						<view>￥210</view>
					</view>
					<view class="plan-item" :class="{active: count==12}" @click="count=12">
						<view>12个月</view>
						<view>
							<text>￥</text>
							<text>360</text>
						</view>
						<view>￥420</view>
					</view>
				</view>
			</view>
			<view class="agreement">
				<text>我已阅读并同意</text>
				<text>《用户协议》</text>
			</view>
			<view class="btn">
				<g-btn title="立即续费" @click="showPayPop = true"></g-btn>
			</view>
		</view>
		<view class="tips">
			<view class="tips-header">会员等级说明</view>
			<view class="tips-main">1、这是会员等级说明，这是权益内容是权益内容说明2、这是权益内容说明这是权益内容权益内容说明3、这是权益内容说明这是权益内容说明这是权益内容说明这是权益内容说明
			</view>
		</view>

		<u-popup v-model="showPayPop" mode="bottom">
			<view class="payPop">
				<view class="payPop-header app_flex-aicjcsb">
					<text>支付方式</text>
					<icon type="clear" color="#ccc" size="19" @click="showPayPop = false"></icon>
				</view>
				<view class="payPop-main">
					<u-radio-group v-model="payWay" size="38">
						<view class="payPop-item app_flex-aicjcsb">
							<view class="payPop-item_L app_flex-aic">
								<image src="../../static/images/icon-logo-zfb.png" mode=""></image>
								<text>支付宝</text>
							</view>
							<view class="payPop-item_R">
								<u-radio name="zfb"></u-radio>
							</view>
						</view>
						<view class="payPop-item app_flex-aicjcsb">
							<view class="payPop-item_L app_flex-aic">
								<image src="../../static/images/icon-logo-wx.png" mode=""></image>
								<text>微信</text>
							</view>
							<view class="payPop-item_R">
								<u-radio name="wx"></u-radio>
							</view>
						</view>
					</u-radio-group>
				</view>
				<view class="payPop-footer app_flex-aicjcsb">
					<view class="payPop-total">
						<text>实付款:</text>
						<text>￥35</text>
					</view>
					<g-btn @click="topup" title="确认支付" :customstyle="{width: '220rpx', height: '80rpx'}"></g-btn>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showPayPop: false,
				payWay: 'zfb',
				count: 1,
				isVipFlag: false
			}
		},
		onLoad(options) {
			this.updateVipFlag()
		},
		methods: {
			updateVipFlag() {
				this.$store.dispatch('updateUserInfo').then(userInfo => {
					// console.log(userInfo);
					if (userInfo && userInfo.vipExpireTime) {
						let timestamp = new Date(userInfo.vipExpireTime).getTime()
						let currentTimestamp = new Date().getTime()
						if (currentTimestamp < timestamp) {
							this.isVipFlag = true
						}
					}
				})
			},
			getOpenid() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: (res) => {
							resolve(res.authResult.openid)
						},
						complete(res) {
							console.log(res);
						}
					})
				})
			},
			async topup() {
				try {
					let reqParams = {
						count: this.count,
						payType: this.payWay == 'zfb' ? 1 : 2
					}
					// reqParams.openid = 'oubh959FniprbS2guWe9_QqhBhVc'
					if (this.payWay == 'wx') {
						reqParams.openid = await this.getOpenid()
					}
					console.log(reqParams);
					this.$g_api.promotionVipPay(reqParams).then(async ({
						data
					}) => {
						console.log(data);
						try {
							if (data) {
								if (this.payWay == 'zfb') { // 支付宝
									await this.$g_pay.requestPayment({
										provider: 'alipay',
										orderInfo: data.orderInfo
									})
								} else if (this.payWay == 'wx') { // 微信支付
									// await this.$g_pay.requestPayment(data)
									await this.$g_pay.requestPayment({
										provider: 'wxpay',
										orderInfo: JSON.stringify(data)
									})
								}
							} else {
								throw '充值失败'
							}
							// uni.$emit('update-dataBoard')
							uni.$emit('update-takeOrder-vipFlag')
							this.updateVipFlag()
							uni.showToast({
								title: '充值成功'
							})
						} catch (err) {
							console.log(err);
							uni.showToast({
								title: err,
								icon: 'none'
							})
						}
					}).catch(err => {
						console.log(err);
					})
				} catch (e) {
					console.log(e);
					//TODO handle the exception
				}

			}
		},
	}
</script>
<style scoped lang="scss">
	::v-deep .u-radio__label {
		margin: 0 !important;
	}

	.main {
		padding: 20rpx 30rpx 50rpx;
		background-color: #fff;
	}

	.user {
		display: flex;
		align-items: center;
		width: 690rpx;
		padding: 30rpx;
		box-sizing: border-box;
		background: linear-gradient(90deg, #3377FF, #66A6FF);
		box-shadow: 0px 6rpx 14rpx 0px rgba(51, 119, 255, 0.3);
		border-radius: 10rpx;
	}

	.user-avatar {
		image {
			margin-right: 30rpx;
			width: 110rpx;
			height: 110rpx;
			border: 2px solid #fff;
			border-radius: 50%;
			background-color: #ddd;
		}
	}

	.user-R {}

	.username {
		font-size: 36rpx;
	}

	.status {
		margin-top: 16rpx;
		display: inline-block;
		padding: 10rpx 20rpx;
		border-radius: 30rpx;
		line-height: 1;
		font-size: 20rpx;
		color: #CCDDFF;
		background-color: rgba(255, 255, 255, .3);
	}

	.plan {}

	.plan-header {
		margin: 50rpx 0 30rpx;
		font-size: 30rpx;
		line-height: 1;
		font-weight: bold;
	}

	.plan-main {
		display: flex;
		justify-content: space-between;
	}

	.plan-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 210rpx;
		height: 190rpx;
		line-height: 1;
		border-radius: 10rpx;
		border: 1px solid #ccc;

		view {
			&:first-child {
				font-size: 26rpx;
				margin-bottom: 24rpx;
			}

			&:nth-child(2) {
				text {
					color: $g-main-color;

					&:nth-child(1) {
						font-size: 30rpx;
					}

					&:nth-child(2) {
						font-size: 48rpx;
					}
				}
			}

			&:last-of-type {
				margin-top: 18rpx;
				color: #999;
				font-size: 24rpx;
				text-decoration: line-through;
			}
		}

		&.active {
			background-color: #E5EEFF;
			border-color: $g-main-color;

			view {
				&:nth-child(3) {
					color: $g-main-color;
				}
			}
		}
	}

	.agreement {
		margin: 40rpx 0 80rpx;

		text {
			&:last-child {
				color: $g-main-color;
			}
		}
	}

	.btn {}

	.tips {
		padding: 0 30rpx;
	}

	.tips-header {
		margin: 40rpx 0 10rpx;
		font-size: 30rpx;
		font-weight: bold;
	}

	.tips-main {
		font-size: 28rpx;
	}

	.payPop {}

	.payPop-header {
		height: 100rpx;
		padding: 0 30rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.payPop-main {}

	.payPop-item {
		height: 120rpx;
		padding: 0 30rpx;
	}

	.payPop-item_L {
		image {
			margin-right: 30rpx;
			width: 70rpx;
			height: 70rpx;
		}
	}

	.payPop-item_R {}

	.payPop-footer {
		height: 80rpx;
		box-sizing: content-box;
		padding: 70rpx 30rpx 0;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
	}

	.payPop-total {
		text {
			&:first-child {
				font-size: 26rpx;
			}

			&:last-child {
				margin-left: 10rpx;
				font-size: 32rpx;
				// font-weight: bold;
				color: #ff7733;
			}
		}
	}
</style>